/**
 * @author (Gruppe G) 
 * @version (04/16/2012)
 */

package ModelLayer;
import java.util.ArrayList;

public class AddressBook
{
    private static AddressBook instance;
    private ArrayList<Person> AddressCollection;

    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        AddressCollection = new ArrayList<Person>();
    }
    
    /**
     * Creates an instance of the AddressBook collection.
     */
    public static AddressBook getInstance()
    {
        if (instance == null)
        {
            instance = new AddressBook();
        }
        
        return instance;    
    }
    
    /**
     * Creating a person entry in the array.
     */
    public void addPerson(Person newPerson)
    {
        AddressCollection.add(newPerson);
    }
    
    /**
     * Finds a person via there phonenumber in array.
     * 
     * @param phone the phonenumber to be used in search
     */
    public Person findPerson(String phone) 
    {
        Person person = null;
        int index = 0;
        boolean found = false;
        while(index < AddressCollection.size() && !found)
        {
            person = AddressCollection.get(index); 
            if(person.getPhone().equals(phone))
            {
                found = true;
            } 
            else 
            {
                index++;
            }
        }
        return person;
    }
    
    /**
     * Deletes a person via there phonenumber in array.
     * 
     * @param phone the phonenumbe to be used in the deletion
     */
    public boolean deletePerson(String phone) 
    {
        Person person = null;
        int index = 0;
        boolean found = false;
        while(index < AddressCollection.size() && !found)
        {
            person = AddressCollection.get(index); 
            if(person.getPhone().equals(phone))
            {
                found = true;
                AddressCollection.remove(index);
            }
            else
            {
                index++;
            }
        }
        return found;
    }
    
    /**
     * Listes all persons in array.
     */
    public ArrayList<Person> listAllPersons()
    {
        return AddressCollection;
    }

}
